package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;


@Controller
public class UserController {
    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理动态页面...");

        try (Connection connection =DBUtil.getConnection())
        {
            String sql ="SELECT id,username,password,nickname,age FROM userinfo";
            PreparedStatement ps =connection.prepareStatement(sql);
            ResultSet rs =ps.executeQuery();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw =response.getWriter();

            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("    <center>");
            pw.println("        <h1>用户列表</h1>");
            pw.println("        <table border=\"1\">");
            pw.println("            <tr>");
            pw.println("                <td>ID</td>");
            pw.println("                <td>用户名</td>");
            pw.println("                <td>密码</td>");
            pw.println("                <td>昵称</td>");
            pw.println("                <td>年龄</td>");
            pw.println("            </tr>");
            while(rs.next()) {
                int id = rs.getInt("id");
                pw.println("            <tr>");
                pw.println("                <td>"+id+"</td>");
                pw.println("                <td><a href='articleList?userId="+id+"'>"+rs.getString("username")+"</a></td>");
                pw.println("                <td>"+rs.getString("password")+"</td>");
                pw.println("                <td>"+rs.getString("nickname")+"</td>");
                pw.println("                <td>"+rs.getInt("age")+"</td>");
                pw.println("            </tr>");
            }
            pw.println("        </table>");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }




    @RequestMapping("/regUser")
   // public void reg(HttpServletRequest request, HttpServletResponse response){
    public void reg(String username,String password,String nickname,int age, HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始注册");
//        String username =request.getParameter("username");
//        String password =request.getParameter("password");
//        String nickname =request.getParameter("nickname");
//        String ageStr = request.getParameter("age");
        if (username==null ||username.isEmpty()||
            password==null ||password.isEmpty()||
            nickname==null ||nickname.isEmpty()
        ){
            try {
                response.sendRedirect("reg_info_error.html");

            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        System.out.println(username+","+password+","+nickname+","+age);

        //将信息传到表中

        try (Connection connection =DBUtil.getConnection()
        ){
            String sql ="SELECT 1 FROM userinfo WHERE username =? ";
            PreparedStatement ps =connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs =ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("have_user.html");
                return;
            }
            String line ="INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
            PreparedStatement ps1 =connection.prepareStatement(line);
            ps1.setString(1,username);
            ps1.setString(2,password);
            ps1.setString(3,nickname);
            ps1.setInt(4,age);
            int num =ps1.executeUpdate();
            if (num>0){
                response.sendRedirect("reg_success.html");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始登录");
        String username =request.getParameter("username");
        String password =request.getParameter("password");
        if (username==null||username.isEmpty()||password==null||password.isEmpty()){
            try {
                response.sendRedirect("login_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (Connection connection =DBUtil.getConnection()
        ){
            String sql ="SELECT id,username,password,nickname,age FROM userinfo WHERE username=? AND password=? ";
            PreparedStatement ps =connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs =ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("login_success.html");
            }else {
                response.sendRedirect("login_fail.html");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
